package fr.sedoo.sos.data.validation;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateValidator implements CellValidator {

	private DateFormat formatter;
	private boolean mandatory = true;
	private Date lastValue;
	
	public DateValidator(String format) 
	{
		this.formatter = new SimpleDateFormat(format);
	}
	
	public DateValidator(String format, boolean mandatory)
	{
		this(format);
		this.mandatory = mandatory;
	}

	public boolean isValid(String cellValue) 
	{
		if (cellValue == null)
		{
			if (mandatory == true)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		else
		{
			try {
				lastValue = formatter.parse(cellValue);
				return true;
			}
			catch(final ParseException e) 
			{
				return false;
			}
		}
	}
	
	
	public Date getLastValue() {
		return lastValue;
	}
}
